﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using TpBaseObjects.Interfaces;
using TpBaseObjects.Objects;
using TpData.Manager;
using TpGlobalForms.Forms;
using TpGlobalForms.Interfaces;

namespace TpGlobalForms.Controller {

    class OrganisationController : IController {

        #region Members

        private OrganisationForm myForm;

        #endregion

        #region Constructor

        public OrganisationController(ITecForm form) {
            myForm = form as OrganisationForm;
        }

        #endregion

        #region Methods

        public void ChangedPersonsProperties(object sender, PropertyChangedEventArgs e) {
            if (e.PropertyName == "Straße") {

            }
        }

        public void ChangedProperties(object sender, PropertyChangedEventArgs e) {
            if (e.PropertyName == "EPLZ") {
                this.myForm.MyModel.Ort = this.myForm.MyPlzOrtModel[this.myForm.MyModel.EPLZ].Ort;
            }
        }

        public void Dispose() {

        }

        public void LoadModels() {
            this.myForm.MyPlzOrtModel = CommonDataManager.LoadPlzOrtList();
            this.myForm.MyRechtsformModel = OrganisationDataManager.GetRechtsformList();
            this.myForm.MyBerufstitelModel = OrganisationDataManager.GetBerufstitelList();
            this.myForm.MyFunktionsModel = OrganisationDataManager.GetFunktionList();
        }

        public void Save(IBaseObject myObject) {
            Organisation org = myObject as Organisation;
            OrganisationDataManager.SaveOrganisation(org);
        }
        
        #endregion

        #region Functions

        public void BeforeCloseMyForm(object sender, FormClosingEventArgs e) {
            if (this.myForm.MyModel.IsDirty) {
                DialogResult res = MessageBox.Show("Datensatz wurde noch nicht gespeichert. Wollen Sie jetzt speichern?", "Barverkauf", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
                if (res == DialogResult.Yes) {
                    this.Save(this.myForm.MyModel);
                    return;
                }
                if (res == DialogResult.No) {
                    return;
                }
            } else {
                return;
            }
            return;
        }

        #endregion

    }
}
